<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <meta name="description" content="View与APIView的区别

View是Django默认的视图基类，APIView是REST framework提供的所有视图的基类, 继承自Django的View。

APIView与View的不同之处在于：
 1.传入到视图方法中的是REST framework的Request对象，而不是D" />
  

  
  
  
  
  
  
  <title>View与APIView的区别 | 南辞的技术博客</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="View与APIView的区别  View是Django默认的视图基类，APIView是REST framework提供的所有视图的基类, 继承自Django的View。  APIView与View的不同之处在于：  1.传入到视图方法中的是REST framework的Request对象，而不是Django的HttpRequeset对象；">
<meta property="og:type" content="article">
<meta property="og:title" content="View与APIView的区别">
<meta property="og:url" content="https://yanlidoushikeke.gitee.io/myhexo/2021/01/14/View%E4%B8%8EAPIView%E7%9A%84%E5%8C%BA%E5%88%AB/index.html">
<meta property="og:site_name" content="南辞的技术博客">
<meta property="og:description" content="View与APIView的区别  View是Django默认的视图基类，APIView是REST framework提供的所有视图的基类, 继承自Django的View。  APIView与View的不同之处在于：  1.传入到视图方法中的是REST framework的Request对象，而不是Django的HttpRequeset对象；">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2021-01-14T09:21:52.000Z">
<meta property="article:modified_time" content="2021-01-19T12:22:49.267Z">
<meta property="article:author" content="南辞">
<meta name="twitter:card" content="summary">
  
  
    <link rel="icon" href="/tefuir/css/images/favicon.ico">
  
  
<link rel="stylesheet" href="/tefuir/css/style.css">

  

  
  <!-- baidu webmaster push -->
  <script src='//push.zhanzhang.baidu.com/push.js'></script>
<meta name="generator" content="Hexo 5.2.0"><link rel="alternate" href="/tefuir/atom.xml" title="南辞的技术博客" type="application/atom+xml">
</head>
<body class="home blog custom-background custom-font-enabled single-author">
  <div id="page" class="hfeed site">
      <header id="masthead" class="site-header" role="banner">
    <hgroup>
      <h1 class="site-title">
        <a href="/tefuir/" title="南辞的技术博客" rel="home">南辞的技术博客</a>
      </h1>
      
        <h2 class="site-description hitokoto"></h2>
        <script type="text/javascript" src="https://v1.hitokoto.cn/?encode=js"></script>
      
    </hgroup>

    <nav id="site-navigation" class="main-navigation" role="navigation">
            <button class="menu-toggle">菜单</button>
            <a class="assistive-text" href="/#content" title="跳至内容">跳至内容</a><!--TODO-->
            <div class="menu-main-container">
                <ul id="menu-main" class="nav-menu">
                
                    <li class="menu-item menu-item-type-post_type menu-item-object-page"><a href="/tefuir/">Home</a></li>
                
                    <li class="menu-item menu-item-type-post_type menu-item-object-page"><a href="/tefuir/archives">Archives</a></li>
                
                </ul>
            </div>
    </nav>
</header>

      <div id="main" class="wrapper">
        <div id="primary" class="site-content"><div id="content" role="main"><article id="post-View与APIView的区别" class="post-View与APIView的区别 post type-post status-publish format-standard hentry">
    <!---->

      <header class="entry-header">
        
        
  
    <h1 class="entry-title article-title">
      View与APIView的区别
    </h1>
  

        
        <div class="comments-link">
            
            <a href="javascript:void(0);" data-url="https://yanlidoushikeke.gitee.io/myhexo/2021/01/14/View%E4%B8%8EAPIView%E7%9A%84%E5%8C%BA%E5%88%AB/" data-id="ckkttwp6m0001csu0ake2djpr" class="leave-reply bdsharebuttonbox" data-cmd="more">Share</a>
        </div><!-- .comments-link -->
      </header><!-- .entry-header -->

    <div class="entry-content">
      
        <p>View与APIView的区别</p>
<ul>
<li><p>View是Django默认的视图基类，APIView是REST framework提供的所有视图的基类, 继承自Django的View。</p>
</li>
<li><p>APIView与View的不同之处在于：</p>
<p> 1.传入到视图方法中的是REST framework的Request对象，而不是Django的HttpRequeset对象；                                    </p>
<pre><code> 2.视图方法可以返回REST framework的Response对象，视图会为响应数据设置（render）符合前端要求的格式；
 3.任何APIException异常都会被捕获到，并且处理成合适的响应信息；APIException异常捕获
 4.在进行dispatch()分发前，会对请求进行身份认证、权限检查、流量控制</code></pre>
</li>
</ul>
<p>1.View部分代码手动实现增改查</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line">view.py</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">import json</span><br><span class="line">from django.http import HttpResponse</span><br><span class="line">from django.shortcuts import render</span><br><span class="line"><span class="comment"># Create your views here.</span></span><br><span class="line">from django.views import View</span><br><span class="line">from .models import User</span><br><span class="line"></span><br><span class="line">class UserView(View):</span><br><span class="line">    def post(self, request):</span><br><span class="line">        name = request.POST.get(<span class="string">&#x27;name&#x27;</span>)</span><br><span class="line">        password = request.POST.get(<span class="string">&#x27;password&#x27;</span>)</span><br><span class="line">        size = request.POST.get(<span class="string">&#x27;size&#x27;</span>)</span><br><span class="line">        user_obj = User.objects.filter(name=name)</span><br><span class="line">        <span class="keyword">if</span> user_obj:</span><br><span class="line">            <span class="built_in">return</span> HttpResponse(<span class="string">&quot;用户已存在&quot;</span>)</span><br><span class="line">        <span class="keyword">else</span>:</span><br><span class="line">            User.objects.create(</span><br><span class="line">                name=name,</span><br><span class="line">                password=password,</span><br><span class="line">                size=size</span><br><span class="line">            )</span><br><span class="line">        <span class="built_in">return</span> HttpResponse(<span class="string">&#x27;添加成功&#x27;</span>)</span><br><span class="line">    def get(self,request):</span><br><span class="line">         list=[]</span><br><span class="line">         userobj=User.objects.all()</span><br><span class="line">         <span class="keyword">for</span> i <span class="keyword">in</span> userobj:</span><br><span class="line">             list.append(&#123;</span><br><span class="line">                 <span class="string">&#x27;name&#x27;</span>:i.name,</span><br><span class="line">                 <span class="string">&#x27;password&#x27;</span>:i.password,</span><br><span class="line">                 <span class="string">&#x27;size&#x27;</span>:i.size</span><br><span class="line">             &#125;)</span><br><span class="line">         <span class="built_in">return</span> HttpResponse(list)</span><br><span class="line"></span><br><span class="line">    def put(self,request):</span><br><span class="line">        data=request.body</span><br><span class="line">        dict_data=json.loads(data)</span><br><span class="line">        <span class="built_in">print</span>(dict_data)</span><br><span class="line">        User.objects.filter(id=dict_data[<span class="string">&#x27;id&#x27;</span>]).update(password=<span class="string">&#x27;1234&#x27;</span>)</span><br><span class="line">        <span class="built_in">return</span> HttpResponse(<span class="string">&#x27;修改成功&#x27;</span>)</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>2.APIView部分代码序列化自动实现增改查</p>
<ul>
<li>   view.py</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line">class Text3(APIView):</span><br><span class="line">    def post(self,resquest):</span><br><span class="line">        class_name = resquest.POST.get(<span class="string">&#x27;class_name&#x27;</span>)</span><br><span class="line">        address = resquest.POST.get(<span class="string">&#x27;address&#x27;</span>)</span><br><span class="line">        class_obj = Class.objects.filter(class_name=class_name,address=address)</span><br><span class="line">        <span class="keyword">if</span> class_obj:</span><br><span class="line">            <span class="built_in">return</span> Response(<span class="string">&#x27;用户已存在&#x27;</span>)</span><br><span class="line">        obj = ClassRoomSer(data=resquest.data)</span><br><span class="line">        <span class="keyword">if</span> obj.is_valid():</span><br><span class="line">            flay=obj.save()</span><br><span class="line">            <span class="keyword">if</span> flay:</span><br><span class="line">                <span class="built_in">return</span> Response(&#123;<span class="string">&quot;msg&quot;</span>:<span class="string">&quot;成功&quot;</span>,<span class="string">&quot;code&quot;</span>:1000&#125;)</span><br><span class="line">            <span class="built_in">return</span> Response(&#123;<span class="string">&quot;msg&quot;</span>:<span class="string">&quot;shi&quot;</span>,<span class="string">&quot;code&quot;</span>:500&#125;)</span><br><span class="line"></span><br><span class="line">    def get(self,request):</span><br><span class="line">        class_obj = Class.objects.all()</span><br><span class="line">        obj = ClassRoomSer(class_obj,many=True)</span><br><span class="line">        <span class="built_in">return</span> Response(obj.data)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    def put(self,request):</span><br><span class="line">        id = request.data.get(<span class="string">&#x27;id&#x27;</span>)</span><br><span class="line">        data = request.data</span><br><span class="line">        class_obj = Class.objects.get(id=id)</span><br><span class="line">        ser_obj = ClassRoomSer(instance = class_obj,data=data)</span><br><span class="line">        <span class="keyword">if</span> ser_obj.is_valid():</span><br><span class="line">            ser_obj.save()</span><br><span class="line">            <span class="built_in">return</span> Response(ser_obj.data)</span><br><span class="line">        <span class="built_in">return</span> Response(ser_obj.errors)</span><br></pre></td></tr></table></figure>
<ul>
<li>   serializers.py</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># -*- coding: utf-8 -*-</span></span><br><span class="line">from rest_framework import serializers</span><br><span class="line">from . import models</span><br><span class="line">class ClassRoomSer(serializers.Serializer):</span><br><span class="line">	<span class="comment">#查询</span></span><br><span class="line">    class_name = serializers.CharField(max_length=32)</span><br><span class="line">    address = serializers.CharField(max_length=225)</span><br><span class="line"></span><br><span class="line">    <span class="comment">#添加</span></span><br><span class="line">    def create(self, validated_data):</span><br><span class="line">        <span class="built_in">return</span> models.Class.objects.create(**validated_data)</span><br><span class="line">	<span class="comment">#修改</span></span><br><span class="line">    def update(self, instance, validated_data):</span><br><span class="line">        instance.class_name = validated_data.get(<span class="string">&#x27;class_name&#x27;</span>,instance.class_name)</span><br><span class="line">        instance.address = validated_data.get(<span class="string">&quot;address&quot;</span> ,instance.address)</span><br><span class="line">        instance.save()</span><br><span class="line">        <span class="built_in">return</span> instance</span><br></pre></td></tr></table></figure>
      
    </div><!-- .entry-content -->

    <footer class="entry-meta">
    <a href="/tefuir/2021/01/14/View%E4%B8%8EAPIView%E7%9A%84%E5%8C%BA%E5%88%AB/">
    <time datetime="2021-01-14T09:21:52.000Z" class="entry-date">
        2021-01-14
    </time>
</a>
    
    
    </footer>
</article>


    
<nav class="nav-single">
    <h3 class="assistive-text">文章导航</h3>
    
        <span class="nav-previous"><a href="/tefuir/2021/01/14/%E4%B8%AD%E9%97%B4%E4%BB%B6%E4%BB%8B%E7%BB%8D/" rel="prev"><span class="meta-nav">←</span> 中间件介绍</a></span>
    
    
</nav><!-- .nav-single -->







</div></div>
        <div id="secondary" class="widget-area" role="complementary">
  
    <aside id="search" class="widget widget_search"><form role="search" method="get" accept-charset="utf-8" id="searchform" class="searchform" action="//google.com/search">
    <div>
        <input type="text" value="" name="s" id="s" />
        <input type="submit" id="searchsubmit" value="搜索" />
    </div>
</form></aside>
  
    
  
    
  
    
  <aside class="widget">
    <h3 class="widget-title">Recents</h3>
    <div class="widget-content">
      <ul>
        
          <li>
            <a href="/tefuir/2021/04/26/python%E5%9F%BA%E7%A1%80%E7%90%86%E8%AE%BA%E5%AE%9D%E5%85%B8/">python基础理论宝典</a>
          </li>
        
          <li>
            <a href="/tefuir/2021/04/23/python3-Flask%E7%BB%93%E5%90%88Socket-io%E7%AE%80%E5%8D%95%E5%AE%9E%E7%8E%B0%E5%9C%A8%E7%BA%BF%E5%AE%A2%E6%9C%8D%E7%B3%BB%E7%BB%9F/">python3+Flask结合Socket.io简单实现在线客服系统</a>
          </li>
        
          <li>
            <a href="/tefuir/2021/04/23/win10%E7%B3%BB%E7%BB%9F%E4%B8%8B%E5%88%A9%E7%94%A8docker%E9%83%A8%E7%BD%B2gunicorn-Flask%E6%89%93%E9%80%A0%E7%8B%AC%E7%AB%8B%E9%95%9C%E5%83%8F/">win10系统下利用docker部署gunicorn+Flask打造独立镜像</a>
          </li>
        
          <li>
            <a href="/tefuir/2021/04/23/win10%E7%B3%BB%E7%BB%9F%E4%B8%8B%E5%9F%BA%E4%BA%8Edocker%E9%85%8D%E7%BD%AEelasticsearch%E9%85%8D%E5%90%88python3%E8%BF%9B%E8%A1%8C%E5%85%A8%E6%96%87%E6%A3%80%E7%B4%A2/">win10系统下基于docker配置elasticsearch配合python3进行全文检索</a>
          </li>
        
          <li>
            <a href="/tefuir/2021/04/23/%E6%9C%8D%E5%8A%A1%E5%99%A8%E9%97%AE%E9%A2%98/">服务器问题</a>
          </li>
        
      </ul>
    </div>
  </aside>

  
    
  
    
  
</div>
      </div>
      <footer id="colophon" role="contentinfo">
    <p>&copy; 2021 南辞
    All rights reserved.</p>
    <p>Powered by <a href="http://hexo.io/" target="_blank">Hexo</a></p>
</footer>
    <script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"1","bdMiniList":false,"bdPic":"","bdStyle":"2","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='/js/share.js'];</script>

<script src="/js/jquery-3.3.1.min.js"></script>


  
<link rel="stylesheet" href="/tefuir/fancybox/jquery.fancybox.css">

  
<script src="/tefuir/fancybox/jquery.fancybox.pack.js"></script>




<script src="/tefuir/js/script.js"></script>


<script src="/js/navigation.js"></script>

<div id="bg"></div>

  </div>
</body>
</html>